package projectServlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.CompensationDAO;
import dao.CompensationDAOFactory;
import dao.ConnPool;
import dao.DAOException;
import dao.EmployeeDAO;
import dao.EmployeeDAOFactory;
import dao.mysql.CompensationDAOFactoryImpl;
import dao.mysql.EmployeeDAOFactoryImpl;
import dto.CompensationDTO;
import dto.UserDTO;

/**
 * Servlet implementation class CompensationListServlet
 */
//@WebServlet("/CompensationListServlet/*")
public class CompensationListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CompensationListServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request,response);
	}

	private void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		CompensationDAOFactory CFac = new CompensationDAOFactoryImpl();
		CompensationDAO compDAO = CFac.getCompensationDAO();
		EmployeeDAOFactory fac = new EmployeeDAOFactoryImpl();
		EmployeeDAO empDAO = fac.getEmployeeDAO();
		ArrayList<CompensationDTO> compList= new ArrayList<CompensationDTO>();
		HttpSession sess = request.getSession();
		String path = request.getPathInfo();
		System.out.println("PATH IS : " + path);
		//System.out.println(path.equals("/Load"));
		//System.out.println(path.equals("/post"));
		if (path.equals("/Load"))
		{
			try
			{
				String SUserID = (String) request.getSession().getAttribute("id");
				int    UserID  = Integer.parseInt(SUserID);
				compList = compDAO.getAllCompApplication(UserID); // Test code due to no connection pool setup. 
				//compList = compDAO.getAllCompApplication(1003); // test code for manager ID
			}
			catch (DAOException e1)
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			sess.setAttribute("compList", compList);
			
			RequestDispatcher rd = request
					.getRequestDispatcher("/CompensationList.jsp");

			rd.forward(request, response);
		}
		
		if (path.equals("/Approve"))
		{
			UserDTO user = new UserDTO();
			try
			{
				ConnPool.setupConPool(); // Test code due to no connection pool setup. 
				user = empDAO.findUser(Integer.parseInt(request.getParameter("userID")));
			}
			catch (DAOException e1)
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			CompensationDTO comp = new CompensationDTO();
			comp.set_compID(Integer.parseInt(request.getParameter("compID")));
			comp.set_status("Approved");
			
			double compDays = user.get_compensationLeave() + Integer.parseInt(request.getParameter("noOfDays"));
			user.set_compensationLeave(compDays);
			
			try
			{
				empDAO.updateUser(user);
				compDAO.updateCompApplicationStatus(comp);
				
				
				//System.out.println("good here 4");
				String SUserID = (String) request.getSession().getAttribute("id");
				int    UserID  = Integer.parseInt(SUserID);
				compList = compDAO.getAllCompApplication(UserID);//need to put in the manager ID
				sess.setAttribute("compList", compList);
				//System.out.println("good here 5");
			}
			catch (DAOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			RequestDispatcher rd = request
					.getRequestDispatcher("/CompensationList.jsp");

			rd.forward(request, response);
			
		}
		
		if (path.equals("/Reject"))
		{
			try
			{
				ConnPool.setupConPool(); // Test code due to no connection pool setup. 
				
			}
			catch (DAOException e1)
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			CompensationDTO comp = new CompensationDTO();
			comp.set_compID(Integer.parseInt(request.getParameter("compID")));
			comp.set_status("Rejected");
			
			try
			{
				String SUserID = (String) request.getSession().getAttribute("id");
				int    UserID  = Integer.parseInt(SUserID);
				compList = compDAO.getAllCompApplication(UserID);//need to put in the manager ID
				sess.setAttribute("compList", compList);
			}
			catch (DAOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			RequestDispatcher rd = request
					.getRequestDispatcher("/CompensationList.jsp");

			rd.forward(request, response);
		}
	}
}
